package com.cxb.demotest.config;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Xbao
 * @version 1.0
 * @description
 * @date 2021/3/10 11:31
 */
//@Configuration
public class RabbitConfig {

  @Value("${spring.rabbitmq.host}")
  private String host;

  @Value("${spring.rabbitmq.port}")
  private Integer port;

  @Value("${spring.rabbitmq.username")
  private String username;

  @Value("${spring.rabbitmq.password")
  private String password;

  @Value("${spring.rabbitmq.virtual-host")
  private String virtualHost;

  public final static String EXCHANGE_A = "exchange_a";
  public final static String EXCHANGE_B = "exchange_b";

  public final static String QUEUE_A = "queue_a";
  public final static String QUEUE_B = "queue_b";

  public final static String ROUTINGKEY_A = "routingkey_a";
  public final static String ROUTINGKEY_B = "routingkey_b";

  @Bean
  public Channel newRabbitmqChannel(){
    //创建连接
    ConnectionFactory factory = new ConnectionFactory();
    //设置rabbitmq主机名
    factory.setHost(host);
    //创建一个连接
    Connection connection;
    Channel channel = null;
    try {
      connection = factory.newConnection();
      //创建一个通道
      channel = connection.createChannel();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (TimeoutException e) {
      e.printStackTrace();
    }

    return channel;
  }

}
